<?php

namespace app\admin\model;

use think\Model;


class Userallbalance extends Model
{

    

    

    // 表名
    protected $name = 'user_all_balance';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'type_text',
        'create_time_text',
        'from_text'
    ];
    

    
    public function getTypeList()
    {
        return ['in' => __('In'), 'out' => __('Out')];
    }

    public function getFromList()
    {
        return ['charge' => __('Charge'), 'order' => __('Order'), 'send' => __('Send'),'refund'=>'退款'];
    }


    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getFromTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['from']) ? $data['from'] : '');
        $list = $this->getFromList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setCreateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function userBalance($user_id)
    {
       $in = $this->where('type', 'in')->where('user_id', $user_id)->sum('amount');
       $out = $this->where('type', 'out')->where('user_id', $user_id)->sum('amount');

       return formatMoney($in-$out);
    }

    public function cacBalance($user_id,$amount)
    {

        $ratioList = array();

        //金额大于等于
        $typeList = $this->group('ratio')->order('ratio','desc')->select();
        if(count($typeList) == 0) return $ratioList;
        $in = $this->where('type', 'in')->where('user_id', $user_id)->sum('amount');
        $out = $this->where('type', 'out')->where('user_id', $user_id)->sum('amount');

        $balance = $in - $out;
        if($balance <= 0) return $ratioList;

        $tmpAmount = $amount;
        foreach ($typeList as $key=>$val){

            $in = $this->where('type', 'in')->where('ratio',$val['ratio'])->where('user_id', $user_id)->sum('amount');
            $out = $this->where('type', 'out')->where('ratio',$val['ratio'])->where('user_id', $user_id)->sum('amount');
            $obj = array();

            $balance = $in-$out;
            if($balance >= $amount){
                $obj['amount'] = $amount;
                $obj['ratio'] = $val['ratio'];
                $ratioList[] = $obj;
                break;
            }else {

                if($tmpAmount - $balance >=0){
                    $obj['amount'] = $balance;
                    $obj['ratio'] = $val['ratio'];
                    $tmpAmount = $tmpAmount -$balance;
                    if($balance >0){
                        $ratioList[] = $obj;
                    }
                    if($tmpAmount == 0){
                        break;
                    }
                }else{

                    $obj['amount'] = $tmpAmount;
                    $obj['ratio'] = $val['ratio'];
                    if($tmpAmount >0){
                        $ratioList[] = $obj;
                    }
                    $tmpAmount = 0;
                    break;
                }
            }

        }

        return $ratioList;

    }





    public function shopProfit($user_id)
    {
        $in = $this->where('type', 'in')->where('user_id', $user_id)->sum('shop_profit');
        $out = $this->where('type', 'out')->where('user_id', $user_id)->sum('shop_profit');

        return formatMoney($in-$out);
    }

    public function user()
    {
        return $this->belongsTo('User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


}
